var maxScrollSize = 480;
var currentFocus = "button_play";
var previousRelPosition = 0;

function playInit() {
   curMenu = 10;
   getUserAgent();
   setInnerHtml("bitrates",getBitrateImageTag(0));
   
   processIconFocusHandle('PlayIcon','over');
   currentFocus = "button_play";

   setElementVisibility("progressFocus", false);

   var m = getMedia();
   if(m != null) {
      m.onPlayStateChange = onPlayStateChange;
      m.Error = onError;
   }
   tmOut = setInterval("refreshMediaControl();", 1000);  
}

function playUnload() {
   var m = getMedia();
   if(m != null) {m.stop();}
   if ( tmOut != null ) {clearInterval(tmOut);}
   removeEvent(document, "mousemove", motionMoveOnProgressBar);
}

function getMedia() {
   return document.getElementById("media");
}

//RCU key down handle
function processKeyDown(event) {
   var keyCode;
   if(window.event) { // IE
      keyCode = event.keyCode;
   } else if(event.which) { // Netscape/Firefox/Opera
      keyCode = event.which;
   } else {
      alert("Unknown event type.");
      return ;
   }

  //if full media control is now unvisible in full mode, show it and skip first input
   if (playMode=='full') {
      if((keyCode == VK_LEFT || keyCode == VK_RIGHT || keyCode == VK_UP || keyCode == VK_DOWN || keyCode == VK_ENTER) && (document.getElementById("fullMediaControl").style.visibility == "hidden")) {
         fullModeCtrlView = 1;
         refreshMediaControl();
         return;
      }
   }

   for(i=2; i<=4; i++) {processKeyHelpOut(i);}

   fullModeCtrlView = 1;
   refreshMediaControl();
   switch(keyCode) {
      case VK_0 :
      case VK_1 :
      case VK_2 :
      case VK_3 :
      case VK_4 :
      case VK_5 :
      case VK_6 :
      case VK_7 :
      case VK_8 :
      case VK_9 :  
         break;
      
      case VK_UP :
         
         if(currentFocus == "button_back") {
            processKeyHelpOut(5);
            processIconFocusHandle('PlayIcon','over');
            currentFocus = "button_play";
         }
         break;
         
      case VK_PAGE_UP :
         if(playMode=='rel') {
            if(currRelPosition != "play") {relListPageUp(null);}
         }
         break;
         
      case VK_DOWN :

         if (currentFocus == "button_play" || currentFocus == "button_stop" || currentFocus == "button_rew" || currentFocus == "button_ff" || currentFocus == "button_full") {
            /*unfocusAll();
            currentFocus = "button_back";
            processKeyHelpOver(5);*/
         } else if(playMode == "rel") {
            if(currRelPosition == null) {
               currRelPosition = currRelIdx - currRelTopIdx;
            }
         }
         break;
         
      case VK_PAGE_DOWN :
         if(playMode=='rel') {
            if(currRelPosition != "play") {relListPageDown(null);}
         }
         break;
         
      case VK_LEFT :
         // backward 1 %
      if (currentFocus == "full_btn_play") {changeFullBtnFocus("full_btn_stop");} else if (currentFocus == "full_btn_pause") {changeFullBtnFocus("full_btn_play");} else if (currentFocus == "full_btn_rew") {changeFullBtnFocus("full_btn_pause");} else if (currentFocus == "full_btn_ff") {changeFullBtnFocus("full_btn_rew");} else if (currentFocus == "full_btn_option") {changeFullBtnFocus("full_btn_ff");} else if (currentFocus == "full_btn_org_size") {changeFullBtnFocus("full_btn_option");} else if (currentFocus == "button_stop") {
         processIconFocusHandle('StopIcon','out');
         processIconFocusHandle('PlayIcon','over');
         currentFocus = "button_play";
      } else if(currentFocus == "button_rew") {
         processIconFocusHandle('RewindIcon','out'); processIconFocusHandle('StopIcon','over');
         currentFocus = "button_stop";
      } else if(currentFocus == "button_ff") {
         processIconFocusHandle('FastForwardIcon','out'); processIconFocusHandle('RewindIcon','over');
         currentFocus = "button_rew";
      } else if(currentFocus == "button_full") {
         processIconFocusHandle('FullScreenIcon','out'); processIconFocusHandle('FastForwardIcon','over');
         currentFocus = "button_ff";
      } else if(currentFocus == "rel_desc_area") {
         if(playMode == "rel"){
            if(currRelPosition == null) {
               doHighlightRel(null, "play");
               currRelPosition = "play";
            } else if(currRelPosition != "play") {
               previousRelPosition = currRelPosition;
               doHighlightRel(null, "play");
               switchKeyHelpArea("relOnPlay");
            }
            unfocusAll();
            processIconFocusHandle('FullScreenIcon','over');
            currentFocus = "button_full";
        } else {
            unfocusAll();
            processIconFocusHandle('FullScreenIcon','over');
            currentFocus = "button_full";
         }
      }
         break;
         
      case VK_RIGHT :
         // forward 1 %

      if (currentFocus == "full_btn_stop") {changeFullBtnFocus("full_btn_play");} else if(currentFocus == "full_btn_play") {changeFullBtnFocus("full_btn_pause");} else if(currentFocus == "full_btn_pause") {changeFullBtnFocus("full_btn_rew");} else if(currentFocus == "full_btn_rew") {changeFullBtnFocus("full_btn_ff");} else if(currentFocus == "full_btn_ff") {changeFullBtnFocus("full_btn_option");} else if(currentFocus == "full_btn_option") {changeFullBtnFocus("full_btn_org_size");} else if(playMode=='rel' && (currentFocus == "button_full" || currentFocus == "button_back")) {
         if(relCnt > 0) {
            unfocusAll();
            currentFocus = "rel_desc_area";
            doHighlightRel(null, previousRelPosition);
            switchKeyHelpArea("relOnList");
         }
      } else {
         if (currentFocus == "button_play") {
            processIconFocusHandle('PlayIcon','out');
            processIconFocusHandle('StopIcon','over');
            currentFocus = "button_stop";
         } else if(currentFocus == "button_stop") {
            processIconFocusHandle('StopIcon','out');
            processIconFocusHandle('RewindIcon','over');
            currentFocus = "button_rew";
         } else if(currentFocus == "button_rew") {
            processIconFocusHandle('RewindIcon','out');
            processIconFocusHandle('FastForwardIcon','over');
            currentFocus = "button_ff";
         } else if(currentFocus == "button_ff") {
            processIconFocusHandle('FastForwardIcon','out');
            processIconFocusHandle('FullScreenIcon','over');
            currentFocus = "button_full";
         }
      }
         break;
         
      case VK_ENTER :
      if(playMode == 'full') {executeFullBtn(currentFocus);} else if( playMode == 'rel' && currentFocus == "rel_desc_area") {
         if (currRelPosition=="play") {executeAction("play");} else {playRelVideo();}
      } else if (currentFocus == "button_play") {executeAction("playToggle");} else if(currentFocus == "button_stop") {executeAction('stop');} else if(currentFocus == "button_rew") {seekMedia("RE");} else if(currentFocus == "button_ff") {seekMedia("FF");} else if(currentFocus == "button_full") {
         if ( playMode == 'scan' ) {executeAction('full');} else if(playMode == 'full') {executeAction('scan');}
      } else if(currentFocus == "button_back") {processKeyHelp('back');}
         break;
         
      case VK_BACK :
         if(playMode == 'full') {executeAction('scan');} else {executeAction('back');}        
         break;
         
      case VK_RED :
         break;
      case VK_GREEN :
         if( playMode == 'scan' ) {executeAction('full');} else if(playMode == 'full') {executeAction('scan');}
         break;
         
      case VK_YELLOW :
         break;
         
      case VK_BLUE :
         break;
         
      case VK_PLAY :
         if(playMode != "rel") {executeAction('play');}else{executeAction('play');}
         break;
         
      case VK_PAUSE :
         executeAction('pause');
         break;
         
      case VK_STOP :
         executeAction('stop');
         break;
         
      case VK_REWIND :
         if(playMode=='rel') {
            
         } else {seekMedia("RE");}
         break;
         
      case VK_FAST_FWD :
         if(playMode=='rel') {
            
         } else {seekMedia("FF");}
         break;
         
      case VK_INFO :
         document.location.reload();
         break;
      default :
         break;
   }
}

var playMediaId = 0;

// 'full' or 'scan' or 'rel'  
var playMode = "scan";
fullModeCtrlView = 0;

function onError() {
   var media = getMedia();
   var errorMsg = "error occured. (ERRCODE:"+media.error+")";
   //alert(errorMsg);
}

function onPlayStateChange(newState) { 
   var m = getMedia();
   var newState = m.playState;

   if(newState == 0) {
      //when media stop
      //executeAction('stop');   
   } else if(newState==1) {
      //when media playing
      setElementVisibility("loadingIcon",false);
   } else if(newState == 4) {
      //buffring
      setElementVisibility("loadingIcon",true);
   } else if(newState == 5) {/*finish*/}
}

var tmOut = null;
var descScrollActivated = true;

function processIconFocusHandle(divId, action) {
   switch(action) {
      case 'over' :
         if(playMode == "full") {
            var stopElement = document.getElementById("StopIconFull");
            var playElement = document.getElementById("PlayIconFull");
            if( divId == "StopIconFull") {
               playElement.style.zIndex = "10100";
               stopElement.style.zIndex = "10200";
            } else if (divId == "PlayIconFull") {
               playElement.style.zIndex = "10200";
               stopElement.style.zIndex = "10100";
            }
         }           
         setElementBgColor(divId, "rgb(112,37,215)");
         break;
      case 'out' :
         setElementBgColor(divId, "rgb(20,22,19)");            
         break;
      default :
         break;
   }
}

function executeAction(action, position) {
   var m = getMedia();
   var v = document.getElementById("mediaDiv");
   var li = document.getElementById("loadingIcon");
   if(playMode=='full') {
      switch(action) {
         case 'playToggle' :
            if(m.playState == 1) {
               m.play(0); //pause
               setImgSrc('playCtrlFull', "http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_ICON_PLAY.png");
            } else {
               m.play(1); // play
               setImgSrc('playCtrlFull', "http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_ICON_PAUSE.png");
            }
            break;
         case 'play' :
            m.play(1); // play
            setImgSrc('playCtrlFull', "http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_ICON_PAUSE.png");
            break;
         case 'pause' :
            m.play(0); //pause
            setImgSrc('playCtrlFull', "http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_ICON_PLAY.png");
            break;
         case 'stop' :
            m.stop();
            seekByRCUDelaySec = -1;
            seekByRCUPosition = -1;
            updateFullModeBarElement(0, 0, 1);
            goScanMode(v, li, m);
            updateBarElement(0, 0, 1);
            setTdElementText("playTimeCur", getTimeText(0));
            setTdElementText("playTimeTotal", "/ "+getTimeText(0));
            setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PLAY.png");
            goRelatedModeFromScanMode();
            processIconFocusHandle('PlayIcon', 'over');
            break;
         case 'scan' :
            goScanMode(v, li, m);
            break;
         case 'option' :
            window.NetCastLaunchQMENU();
            break;
         case 'ratio' :
            window.NetCastLaunchRATIO();
            break;
         case '3d' :
            window.NetCastLaunch3DMENU();
            break;
         case 'back' :
            //window.history.back();
            jQuery.globalEval($('#control_return').attr('onclick'));
            break;
         default :
           break; 
      }     
   } else if(playMode=='scan') {
      switch(action) {
         case 'playToggle' :
            if(m.playState == 1) {
               m.play(0); //pause
               setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PLAY.png");
            } else {
               m.play(1); // play
               setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PAUSE.png");
            }
            break;
         case 'play' :
            m.play(1); // play
            setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PAUSE.png");
            processIconFocusHandle('PlayIcon', 'over');
            break;
         case 'pause' :
            m.play(0); //pause
            setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PLAY.png");
            processIconFocusHandle('PlayIcon', 'over');
            break;
         case 'stop' :
            m.stop();
            seekByRCUDelaySec = -1;
            seekByRCUPosition = -1;
            updateBarElement(0, 0, 1);
            setTdElementText("playTimeCur", getTimeText(0));
            setTdElementText("playTimeTotal", "/ "+getTimeText(0));
            goRelatedModeFromScanMode();
            setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PLAY.png");
            processIconFocusHandle('PlayIcon', 'over');
            break;
         case 'full' :
            goFullScreenMode(v, li, m);
            break;
         case 'back' :
            //window.history.back();
            jQuery.globalEval($('#control_return').attr('onclick'));
            break;
         default :
            break;
      }
   } else if(playMode=='rel') {
      switch(action) {
         case 'playToggle' :
            if(m.playState != 1) {
               m.play(1); // play
               setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PAUSE.png");
               goScanModeFromRelatedMode();
            }
            processIconFocusHandle('PlayIcon', 'over');
            break;
         case 'play' :
            m.play(1); // play
            setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PAUSE.png");
            goScanModeFromRelatedMode();
            processIconFocusHandle('PlayIcon', 'over');
            break;
         case 'stop' :
            setImgSrc('playCtrl', "http://static.you2play.com/xl/images/pg_bar/PGBAR_ICON_PLAY.png");
            processIconFocusHandle('PlayIcon', 'over');
            break;
         case 'pp' :
            relListPageUp(null);
            break;
         case 'np' :
            relListPageDown(null);
            break;
         case 'back' :
            //window.history.back();
            jQuery.globalEval($('#control_return').attr('onclick'));
            break;
         default :
            break;
      }
   }
}

function goScanMode(mDiv, lIcon, media) {
   //go just scan
   playMode = "scan";
   setElementVisibility("mediaControl",true);

   //jjw_modify
   if(userAgent.search("LG NetCast.TV") > -1) {
      setElementDisplay("fullKeyHelpArea_tv_bg",false);
      setElementDisplay("fullKeyHelpArea_tv",false);
   } else if(userAgent.search("LG NetCast.Media") > -1) {
      setElementDisplay("fullKeyHelpArea_bdp_bg",false);
      setElementDisplay("fullKeyHelpArea_bdp",false);
   }  
   
   setElementVisibility("fullMediaControl",false);
   mDiv.style.top = '137px';
   mDiv.style.left = '37px';
   mDiv.style.width = '640px';
   mDiv.style.height = '360px';
   lIcon.style.top = '240px';
   lIcon.style.left = '282px';
   media.width = 640;

   media.height = 360;  
   refreshMediaControl();
   
   //add as change full screen UX
   changeFullBtnFocus("");
  unfocusAll();
  processIconFocusHandle('PlayIcon','over');
   currentFocus = "button_play";
   
}

function goFullScreenMode(mDiv, lIcon, media) {
   //go full screen
   playMode = "full";

   //jjw_modify
   /*if(userAgent.search("LG NetCast.TV") > -1) {
      setElementDisplay("key_tv_bg",false);
      setElementDisplay("key_tv",false);
      setElementDisplay("fullKeyHelpArea_tv_bg",true);
      setElementDisplay("fullKeyHelpArea_tv",true);
   } else if(userAgent.search("LG NetCast.Media") > -1) {
      setElementDisplay("key_bdp_bg",false);
      setElementDisplay("key_bdp",false);
      setElementDisplay("fullKeyHelpArea_bdp_bg",true);
      setElementDisplay("fullKeyHelpArea_bdp",true);
   }*/

   setElementVisibility("fullMediaControl",true);
   setElementVisibility("mediaControl",false);

   mDiv.style.top = '0px';
   mDiv.style.left = '0px';
   mDiv.style.width = '1280px';
   mDiv.style.height = '720px';
   lIcon.style.left = '559px';
   lIcon.style.top = '279px';
   media.width = 1280;
   media.height = 720;  
   fullModeCtrlView = 1;
   refreshMediaControl();
   
   //add as change full screen UX
   changeFullBtnFocus("full_btn_stop");
}

var relCnt = 0;
var currRelIdx = 0;
var currRelTopIdx = 0;
var currRelPosition = "play";
//var previousRelPosition = 0;

function goRelatedModeFromScanMode() {
   //base on ScanMode
   playMode = "rel";
   //switchKeyHelpArea("relOnPlay");
   setElementVisibility("loadingIcon",false); 
   setElementVisibility("largePlayIcon",true);
  unfocusAll();

  /*
   // relListCnt
   relCnt = relTitle.length;
   if(relCnt == 0 ) {
      setTdElementText("relListCnt", "0/0");
   } else {
      setTdElementText("relListCnt", "1/"+relCnt);
   }
   setElementVisibility("relEtc",true);
   setElementVisibility("relEtcImg",true);
   relEtcVisibleCheck();
   currRelIdx = 0;
   currRelTopIdx = 0;
   fillRelatedList(); 
   currRelPosition = "play";
   doHighlightRel(null, "play");*/
}

function fillRelatedList() {
   var minRel = (relCnt-currRelTopIdx )> 4 ? 4 : (relCnt-currRelTopIdx ) ;
   var idx = currRelTopIdx;
   minRel++;
   for(var i=0; i< minRel ; i++ ) {
      setInnerHtml("relThumenail_"+i , relImg[idx] );
      setTdElementText("relTitle_"+i, relTitle[idx]);
      setTdElementText("relViewCount_"+i, relViewCount[idx]);
      setTdElementText("relUploadId_"+i, relUploadId[idx]);
      
      idx++;
   }

   relEtcVisibleCheck();
}

function relEtcVisibleCheck() {
   if(currRelTopIdx + 4 < relCnt) {
      setElementVisibility("relEtc", true);
      setElementVisibility("relEtcImg", true);
   } else {
      setElementVisibility("relEtc", false);
      setElementVisibility("relEtcImg", false);
   }
   
}

function doHighlightRel(event, idx) {
   //added as ux change
   currentFocus = "rel_desc_area";
   
   if( idx == null || idx == 'undefined') {return;} 
   if ( relCnt < 1 && idx != 'play' && idx != "relEtc") {return;}
   // idx : play or 0~3
   if(currRelPosition !=null && idx != currRelPosition) {
      cancelHighlightRel(null, currRelPosition);
      currRelPosition = idx;
   }
   if(idx == 'play') {
     setElementBackground("largePlayIcon", "url(http://static.you2play.com/xl/images/common/BACKGROUND_LARGE_PLAY_NORMAL.png)");
    processIconFocusHandle('PlayIcon','over');
    currentFocus = "button_play";

      if(curHeyHelpState != 'relOnPlay') {switchKeyHelpArea('relOnPlay');}
      for(var i=0; i<4 ;i++) {
         setRelFocus(false, i);
      }
      setrelEtcFocus(false);
   }else if(idx == "relEtc"){
      setrelEtcFocus(true);
     setTdElementText("relListCnt", (Number(currRelTopIdx) + 5) + "/"+relCnt);
      //added as ux change
      currRelIdx = Number(currRelTopIdx) + 4;
      currRelPosition = 4;
      updateRelListCnt(currRelIdx);
   }else {
      if(curHeyHelpState != 'relOnList') {switchKeyHelpArea('relOnList');}
      for(var i=0; i<4 ;i++) {
         if(idx == i) {
            setElementBackground("largePlayIcon", "url(http://static.you2play.com/xl/images/common/BACKGROUND_LARGE_PLAY_NORMAL.png)");
           unfocusAll();
            continue;
         }
         setRelFocus(false, i);
      }
      setrelEtcFocus(false);
      setRelFocus(true, idx);

      if(event != null) {
         currRelIdx = new Number(currRelTopIdx) + new Number(idx);
         currRelPosition = idx;
      }
      
      updateRelListCnt(currRelIdx);
      
   }
}

// update related-video count and scroll position  
function updateRelListCnt(currIdx) {
   var cri = currIdx;
   cri++;
   setTdElementText("relListCnt", cri+"/"+relCnt);
   var scrollTop = Math.floor(  ( new Number(currIdx) / new Number( new Number(relCnt)-1 )) * maxScrollSize  );
   if(scrollTop == 0){scrollTop = -10;}
   document.getElementById("scrollImg").style.top = scrollTop + "px";
}

function cancelHighlightRel(event, idx) {
   if( idx == null || idx == 'undefined') {return;}
   if ( relCnt < 1 && idx != 'play') {return;}
   // idx : play or 0~3
   currRelPosition = null;
   if(idx == 'play') {
      setElementBackground("largePlayIcon", "url(http://static.you2play.com/xl/images/common/BACKGROUND_LARGE_PLAY_NORMAL.png)");
       unfocusAll();
   } else if(idx == 'relEtc'){setrelEtcFocus(false);}else {
      setRelFocus(false, idx);
      if(event != null) {
         currRelIdx = new Number(currRelTopIdx) + new Number(idx);
         previousRelPosition = idx;
      }
   }
}

function setRelFocus(isFocus, idx) {
   if(isFocus) {
      setElementBackground("rel_"+idx, "url('http://static.you2play.com/xl/images/rel_video/RELATED_FOCUS_LONG.png')" );
      /*document.getElementById("relTitle_"+idx).style.fontSize="34px";
      document.getElementById("relTitle_"+idx).style.top="1px";
      document.getElementById("relTitle_"+idx).style.color="rgb(250,250,250)";
      document.getElementById("relViewCount_"+idx).style.color="rgb(180,185,252)";
      document.getElementById("relUploadId_"+idx).style.color="rgb(180,185,252)";*/
   } else {
      setElementBackground("rel_"+idx, "" );
      /*document.getElementById("relTitle_"+idx).style.fontSize="30px";
      document.getElementById("relTitle_"+idx).style.top="3px";
      document.getElementById("relTitle_"+idx).style.color="rgb(157,157,157)";
      document.getElementById("relViewCount_"+idx).style.color="rgb(110,110,110)";
      document.getElementById("relUploadId_"+idx).style.color="rgb(110,110,110)";*/
   }
}

function setrelEtcFocus(isFocus) {
   if(isFocus) {
      setElementBackground("relEtc", "url('http://static.you2play.com/xl/images/rel_video/RELATED_FOCUS_LONG.png')" );
      /*document.getElementById("relTitle_4").style.fontSize="34px";
      document.getElementById("relTitle_4").style.top="1px";
      document.getElementById("relTitle_4").style.color="rgb(250,250,250)";
      document.getElementById("relViewCount_4").style.color="rgb(180,185,252)";
      document.getElementById("relUploadId_4").style.color="rgb(180,185,252)";*/
    curHeyHelpState = "relOnList";
    currRelPosition = 4;
   } else {
      setElementBackground("relEtc", "" );
      /*document.getElementById("relTitle_4").style.fontSize="30px";
      document.getElementById("relTitle_4").style.top="3px";
      document.getElementById("relTitle_4").style.color="rgb(157,157,157)";
      document.getElementById("relViewCount_4").style.color="rgb(110,110,110)";
      document.getElementById("relUploadId_4").style.color="rgb(110,110,110)";*/
   }
}

function clickRelEtc(){playRelVideo();}

function playRelVideo() {
   document.location.href = "play.html?id="+relRefId[currRelIdx];
}

function relListPageUp(event) {
   if(event != null) {
      var temp = currRelPosition;
      cancelHighlightRel(null, currRelPosition);
      currRelPosition = temp;
   }
   var nextTop = new Number(currRelTopIdx) - 4;
   if(nextTop < 0) {
      currRelTopIdx = 0;
      cancelHighlightRel(null, currRelPosition);
      currRelPosition = 0;
      currRelIdx = 0;
   } else {
      currRelTopIdx = nextTop;
      currRelIdx = new Number(currRelIdx) - 4;
   }
   refillRelatedList();
}

function relListPageDown(event) {
   if(event != null) {
      var temp = currRelPosition;
      cancelHighlightRel(null, temp);
      currRelPosition = temp;
   }
   var nextTop = new Number(currRelTopIdx) + 4;
   if(nextTop > new Number(relCnt) -4 ) {
      currRelTopIdx = new Number(relCnt) -4;
      cancelHighlightRel(null, currRelPosition);
      currRelPosition = 0;
      currRelIdx = currRelTopIdx;
   } else {
      currRelTopIdx = nextTop;
      currRelIdx = new Number(currRelIdx) + 4;
   }
   refillRelatedList();
}

function refillRelatedList() {
   fillRelatedList();
   currRelIdx = new Number(currRelTopIdx) + new Number(currRelPosition);
   if(currRelPosition!=null) {
      doHighlightRel(null, currRelPosition);
   }
   relEtcVisibleCheck();
}

function doHighlightRelNextPage() {
   setImgSrc("relNextImg", "http://static.you2play.com/xl/images/rel_video/NEXT_FOCUS.jpg");
}

function cancelHighlightRelNextPage() {
   setImgSrc("relNextImg", "http://static.you2play.com/xl/images/rel_video/NEXT.jpg");
}

function doHighlightRelPrevPage() {
   setImgSrc("relPrevImg", "http://static.you2play.com/xl/images/rel_video/PREV_FOCUS.jpg");
}

function cancelHighlightRelPrevPage() {
   setImgSrc("relPrevImg", "http://static.you2play.com/xl/images/rel_video/PREV.jpg");
}

function goScanModeFromRelatedMode() {
   playMode = "scan";
   setElementVisibility("largePlayIcon",false);
   unfocusAll();

   setElementVisibility("relEtc",false);
   setElementVisibility("relEtcImg",false);

   setTdElementText("relListCnt", " ");

   currRelIdx = 0;
   currRelTopIdx = 0;
   previousRelPosition = 0;
   
}

var curHeyHelpState = 'relOnPlay';

//switch between rel(play, rel) and scan mode.
function switchKeyHelpArea(pm) {
   curHeyHelpState = pm;
   switch(pm) {
      case 'relOnPlay' :
         //jjw_modify
         if(userAgent.search("LG NetCast.TV") > -1) {
            setElementDisplay("key_tv_bg",true);
            setElementDisplay("key_tv",true);
            setElementDisplay("fullKeyHelpArea_tv_bg",false);
            setElementDisplay("fullKeyHelpArea_tv",false);
         } else if(userAgent.search("LG NetCast.Media") > -1) {
            setElementDisplay("key_bdp_bg",true);
            setElementDisplay("key_bdp",true);
            setElementDisplay("fullKeyHelpArea_bdp_bg",false);
            setElementDisplay("fullKeyHelpArea_bdp",false);
         }  
         break;
      case 'relOnList' :
         break;
      case 'scan' :
         setKeyHelpAction("0", false, null, null);
         setKeyHelpAction("1", false, null, null);
         setKeyHelpAction("2", true, "executeAction('full')", "<img src=\"http://static.you2play.com/xl/images/keyhelp/KEYHELP_ICON_GREEN.png\" style=\" vertical-align: middle;\"> Full Screen");
         break;
      default :
         break;
   }
}

function getTimeText(t) {
   var pt = new Number(t) / 1000;
   var hh = Math.floor(pt / 3600);
   var mm = Math.floor((pt % 3600) / 60);
   var ss = Math.floor(pt % 60);
   return "" + (hh < 10 ? "0"+hh : hh) + ":"+ (mm < 10 ? "0"+mm : mm) +":"+ (ss < 10 ? "0"+ss : ss);
}

function updateBarElement(bufferEnd, playPosition, playTime) {
   //bufferBar width = 300px : 100%
   var bufferBarMax = 460;   //364
   var bufferBarPx = 0;
   if(bufferEnd == -1) {
      bufferBarPx = bufferBarMax;
   } else {
      bufferBarPx = Math.round((bufferEnd/playTime)*bufferBarMax);
   }
   //document.getElementById("debug").innerHTML = '<br />bufferBarPx = ' + bufferBarPx + '<br />bufferEnd = ' + bufferEnd + '<br />playPosition = ' + playPosition + '<br />playTime = ' + playTime;
   document.getElementById("bufferBar").style.width = bufferBarPx + "px" ;
   var progressBarPx =  Math.round((playPosition/playTime)*bufferBarMax);
   document.getElementById("progressBar").style.width = progressBarPx + "px" ;
   if(seekByRCUDelaySec == -1) {
      progressBarPx = new Number(progressBarPx)+11;
      document.getElementById("progressPointer").style.left = progressBarPx + "px" ;
   }
}

var maxBufferwidth = 708;
function updateFullModeBarElement(bufferEnd, playPosition, playTime) {
   var bufferBarPx = 0;
   if(bufferEnd == -1) {
      bufferBarPx = maxBufferwidth;
   } else {
      bufferBarPx = Math.round((bufferEnd/playTime)*maxBufferwidth);
   }
   document.getElementById("bufferBarFull").style.width = bufferBarPx + "px" ;
   var progressBarPx =  Math.round((playPosition/playTime)*maxBufferwidth);
   document.getElementById("progressBarFull").style.width = progressBarPx + "px" ;
   if(seekByRCUDelaySec == -1) {
      progressBarPx = new Number(progressBarPx) + 297 - 7;
      document.getElementById("progressPointerFull").style.left = progressBarPx + "px" ;
   }
}

var seekByRCUDelaySec = -1;
var seekByRCUPosition = -1;
var seekOnePercent = 0;
var seekFivePercent = 0;

function seekMedia(action) {
   
   var m = getMedia();
   var pi = m.mediaPlayInfo();
   var duration = pi.duration;
   if(pi.currentPosition > pi.duration) {
      duration = pi.currentPosition;
   }
   if(seekByRCUPosition == -1 ) {
      seekOnePercent = Math.floor(duration/100);
      seekFivePercent = Math.floor(duration/20);
      seekByRCUPosition = pi.currentPosition;
   } else if (seekByRCUPosition == 0) {
      seekOnePercent = Math.floor(duration/100);
      seekFivePercent = Math.floor(duration/20);
   }
   seekByRCUDelaySec = 2;
   
   switch(action) {
      case 'LEFT' :
         // -1%
         seekByRCUPosition = new Number(seekByRCUPosition) - seekOnePercent;
         if(seekByRCUPosition < 0 ) {
            seekByRCUPosition = 0;
         } 
         break; 
      case 'RIGHT' :
         // +1 &
         seekByRCUPosition = new Number(seekByRCUPosition) + seekOnePercent;
         if(seekByRCUPosition > duration ) {
            seekByRCUPosition = duration;
         }
         break; 
      case 'FF' :
         // +5 %
         seekByRCUPosition = new Number(seekByRCUPosition) + seekFivePercent;
         if(seekByRCUPosition > duration ) {
            seekByRCUPosition = duration;
         }
         break; 
      case 'RE' :
         // -5 %
         seekByRCUPosition = new Number(seekByRCUPosition) - seekFivePercent;
         if(seekByRCUPosition < 0 ) {
            seekByRCUPosition = 0;
         } 
         break;
      default :
         return; 
   }
   
   if(playMode=="scan") {
      var pp =  Math.round((seekByRCUPosition/duration)*460);   //364
      pp = new Number(pp)+11;
      document.getElementById("progressPointer").style.left = pp + "px" ;
   }else if(playMode == "full") {
      var pp =  Math.round((seekByRCUPosition/duration)*maxBufferwidth);
      pp = new Number(pp) + 297 - 7;
      document.getElementById("progressPointerFull").style.left = pp + "px" ;
   }
}

// refresh per 1000ms
function refreshMediaControl() {
   var m = getMedia();
   var pi = m.mediaPlayInfo();
   var br = new Number(pi.bitrateInstant) / 1000000;
   var level = 0; 
   for( var i=0; i< 5 ; i++ ) {
      if(br >= ( 2 * i ) + 2 ) {level++;} 
   }
   
   var currentPosition = 0;
   var duration = 0;
   
   if(pi.currentPosition > pi.duration ) {
      currentPosition = pi.currentPosition;
      duration = pi.currentPosition;
   } else {
      currentPosition = pi.currentPosition;
      duration = pi.duration;
   }
   if ( (currentPosition/1000) >= (duration/1000) - 1 && currentPosition > 0) {executeAction('stop');}
   
   if(playMode=='scan') {
      //bitrates 
      setInnerHtml("bitrates",getBitrateImageTag(level));
      //updateBarElement
      updateBarElement(pi.bufEnd, currentPosition, duration);
      //playTime  0:00:00/0:00:00 
      setTdElementText("playTimeCur", getTimeText(currentPosition));
      setTdElementText("playTimeTotal", "/ "+getTimeText(duration));
      setTdElementText("mediaTime", getTimeText(duration));
     
   } else if (playMode=='full') {
      
      if(fullModeCtrlView == 0 ) {
         // control is hiding.. 
         return;
      } else if (fullModeCtrlView == 1) {
         
         if(userAgent.search("LG NetCast.TV") > -1) {    
            setElementDisplay("fullKeyHelpArea_tv_bg",true);
            setElementDisplay("fullKeyHelpArea_tv",true);
         } else if(userAgent.search("LG NetCast.Media") > -1) {            
            setElementDisplay("fullKeyHelpArea_bdp_bg",true);
            setElementDisplay("fullKeyHelpArea_bdp",true);
         }
         setElementVisibility("fullMediaControl",true);
         fullModeCtrlView ++;
      } else if (fullModeCtrlView == 7) {
         if(userAgent.search("LG NetCast.TV") > -1) {          
            setElementDisplay("fullKeyHelpArea_tv_bg",false);
            setElementDisplay("fullKeyHelpArea_tv",false);
         } else if(userAgent.search("LG NetCast.Media") > -1) {            
            setElementDisplay("fullKeyHelpArea_bdp_bg",false);
            setElementDisplay("fullKeyHelpArea_bdp",false);
         }
         setElementVisibility("fullMediaControl",false);
         window.NetCastMouseOff();
         fullModeCtrlView = 0;
         return;
      } else {
         fullModeCtrlView ++;
      }
      
      //bitrates 
      setInnerHtml("bitratesDiv",getBitrateImageTag(level));
      //updateBarElement
      updateFullModeBarElement(pi.bufEnd, currentPosition, duration);
      //playTime  0:00:00/0:00:00 
      setTdElementText("playTimeCurFull", getTimeText(currentPosition).substring(3,8));
      setTdElementText("playTimeTotalFull", "/ "+getTimeText(duration).substring(3,8));
   }
   
   if(seekByRCUDelaySec > 0) {
      seekByRCUDelaySec --;
   } else if (seekByRCUDelaySec == 0) {
      //seek
      m.seek(seekByRCUPosition);
      seekByRCUDelaySec = -1;
      seekByRCUPosition = -1;
   }
}

function progressIconHandle(event, action) {
   var m = getMedia();
   // it is only activated in play or pause state  
   var pState = m.playState;
   if( pState != 1 && pState != 2) {return;}
   var postFix = "";
   var isFullMode = false;
   if(playMode == 'full') {
      postFix = 'Full';
      isFullMode = true;
      fullModeCtrlView = 1;
   }
   switch(action) {
      case 'over' :
         setElementVisibility("progressFocus"+postFix, true);
         motionMoveOnProgressBar(event);
         addEvent(document, "mousemove", motionMoveOnProgressBar);
         break;
      case 'out' :
         break;
      case 'down' :
         var l = document.getElementById("progressFocus"+postFix).style.pixelLeft;
         if(isFullMode) {
            l = 300 * (Math.round(new Number(l) - 277 ) / 708);
         } else {
            l = 300 * (Math.round(new Number(l) + 4) / 460);   //364
         }
         if(l < 0){l = 0;}
         else if(l > 300){l = 300;}
         playMediaAtPosition(l);
         break;
      default :
         break;
   } 
}

function motionMoveOnProgressBar(event) {
   var x = event.clientX;
   var y = event.clientY;
   if(playMode == 'full') {
      
      if(y < 580  || y > 640 || x > 1005 || x < 297 ) {
         setElementVisibility("progressFocusFull", false);
         removeEvent(document, "mousemove", motionMoveOnProgressBar);
      } else { 
         var p = new Number(x) - 20;   
         document.getElementById("progressFocusFull").style.left = p + "px";
      }
   } else {
      if( x > 443 || y < 420 || y > 480 || x < 79 ) {
         setElementVisibility("progressFocus", false);
         removeEvent(document, "mousemove", motionMoveOnProgressBar);
      } else {  
         var p = new Number(x) - 82;
         document.getElementById("progressFocus").style.left = p + "px";
      }
   }
}

function playMediaAtPosition(position) {
   // position : 0 ~ 300;
   var m = getMedia();
   var pi = m.mediaPlayInfo();
   var playTime = pi.duration;
   if(pi.currentPosition > pi.duration ) {
      playTime = pi.currentPosition;
   }
   var p = Math.round( new Number((playTime) * (position/300)));
   m.seek(p);
   refreshMediaControl();
}

function clickFullScreen(event) {
   fullModeCtrlView = 1;
   refreshMediaControl();
}


/**
 * media description scroll
 */
  
var descScrollImgTop = 0;



var descScrollY = 0;
function processDescScrollDown(event) {
   var e = document.getElementById("scrollImg");
   descScrollY = event.clientY - e.offsetTop;
}

function unfocusAll(){
  processIconFocusHandle('PlayIcon','out');
  processIconFocusHandle('StopIcon','out');
  processIconFocusHandle('RewindIcon','out');
  processIconFocusHandle('FastForwardIcon','out');
  processIconFocusHandle('FullScreenIcon','out');
   processKeyHelpOut(5);
  setElementBackground("largePlayIcon", "url(http://static.you2play.com/xl/images/common/BACKGROUND_LARGE_PLAY_NORMAL.png)");

  for(var i = 0 ; i < 4 ; i++){
   setRelFocus(false, i);
  }
   setrelEtcFocus(false);
}

var cfp = "";
function changeFullBtnFocus(cf){
  //unfocus current
   if(cfp == "full_btn_stop" || cfp == ""){setElementBackground("progressStopFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_STOP_BTN_N.png) no-repeat");}
   if(cfp == "full_btn_play" || cfp == ""){setElementBackground("progressPlayFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_PLAY_BTN_N.png) no-repeat");}
   if(cfp == "full_btn_pause" || cfp == ""){setElementBackground("progressPauseFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_PAUSE_BTN_N.png) no-repeat");}
   if(cfp == "full_btn_rew" || cfp == ""){setElementBackground("progressRewFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_REW_BTN_N.png) no-repeat");}
   if(cfp == "full_btn_ff" || cfp == ""){setElementBackground("progressFfFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_FF_BTN_N.png) no-repeat");}
   if(cfp == "full_btn_option" || cfp == ""){setElementBackground("fullScreenOption", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_BTN_N.png) no-repeat");}
   if(cfp == "full_btn_org_size" || cfp == ""){setElementBackground("fullScreenOrgSize", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_BTN_N.png) no-repeat");}

   //focus target
   if(cf == "full_btn_stop"){setElementBackground("progressStopFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_STOP_BTN_F.png) no-repeat");}
   else if(cf == "full_btn_play"){setElementBackground("progressPlayFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_PLAY_BTN_F.png) no-repeat");}
   else if(cf == "full_btn_pause"){setElementBackground("progressPauseFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_PAUSE_BTN_F.png) no-repeat");}
   else if(cf == "full_btn_rew"){setElementBackground("progressRewFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_REW_BTN_F.png) no-repeat");}
   else if(cf == "full_btn_ff"){setElementBackground("progressFfFull", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_FF_BTN_F.png) no-repeat");}
   else if(cf == "full_btn_option"){setElementBackground("fullScreenOption", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_BTN_F.png) no-repeat");}
   else if(cf == "full_btn_org_size"){setElementBackground("fullScreenOrgSize", "url(http://static.you2play.com/xl/images/pg_bar/FULL_PGBAR_BTN_F.png) no-repeat");}

   //save current
   cfp = cf;
   currentFocus = cf;
}

function executeFullBtn(cf){
   if(cf == "full_btn_stop"){executeAction('stop');}
   else if(cf == "full_btn_play"){executeAction('play');}
   else if(cf == "full_btn_pause"){executeAction('pause');}
   else if(cf == "full_btn_rew"){seekMedia("RE");}
   else if(cf == "full_btn_ff"){seekMedia("FF");}
   else if(cf == "full_btn_option"){executeAction('option');}
   else if(cf == "full_btn_org_size"){executeAction('scan');}
}